// pagesUser/collect/index.js
const app = getApp()
Page({

  ...app.globalData.function,
  data: {
    ...app.globalData.config,
    list:{},
    latitude:39.908860,
    longitude:116.397390,
    markers: [{     
      iconPath: "/images/marker.png",
      id: 0,
      latitude: 39.908860,
      longitude: 116.397390,
      width: 30,
      height: 30,
      label: {
        content: "这里是标签",
        color: "#ffffff",
        fontSize: 14,
        borderRadius: 5,
        bgColor: "#000000",
        padding: 5
      }
    }],
    search:"",
    userInfo:{}

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLocation()  
  },
  
  onShow(){
    this.setData({
      userInfo:this.getStorage('user_info')
    })
    setTimeout(()=>{
      this.selectComponent("#page").hideLoading()
    },1000)
  },

 


  getLocation(){
    
    var that=this;
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        var lat = res.latitude
        var lng = res.longitude
        that.setData({ latitude:lat, longitude: lng })  
        let jsonData={
          search:this.data.search,
          lat:this.data.latitude,
          lng: this.data.longitude,
        }
        this.shopMoreList(jsonData).then(res=>{
          if(res.resCode=='9999' && res.data.list.length>0){
            this.setData({
              list:res.data.list,
              latitude:res.data.list[0].lng,
              longitude:res.data.list[0].lat,
              markers:[{
                latitude:res.data.list[0].lng,
                longitude:res.data.list[0].lat,
                label: {
                  content: res.data.list[0].storage_shop_name,
                }
              }]
    
            })        
          }else{
            console.log(22)
            this.setData({
              list:[]
            })
          }
        })
         
      },
      fail: function() {
        wx.showToast({
          title: '重新进入小程序',
          icon:"none",
        })
      }
    })  
  },


  
  position(options){
    this.setData({
      latitude:options.currentTarget.dataset.info.lng,
      longitude:options.currentTarget.dataset.info.lat,
      markers:[{
        latitude:options.currentTarget.dataset.info.lng,
        longitude:options.currentTarget.dataset.info.lat,
        label: {
          content: options.currentTarget.dataset.info.storage_shop_name,
        }
      }]
    })
    console.log(options)

  },
  round_retain:function(num, n) {
    // num： 要处理的数  n： 保留位数
    // 十进制向右移动n为，而后利用 Math.round() 四舍五入，再向左移动n位，最后利用 toFixed() 保留小数
    return (Math.round(num * Math.pow(10, n)) / Math.pow(10, n)).toFixed(n);
},
openMap: function (options) {  
 let shopInfo=options.currentTarget.dataset.item

 wx.openLocation({
  latitude: Number(this.round_retain(shopInfo.lng,5)), // 纬度，范围为-90~90，负数表示南纬
  longitude: Number(this.round_retain(shopInfo.lat,5)), // 经度，范围为-180~180，负数表示西经
  scale: 25, // 缩放比例
  name:shopInfo.storage_shop_name,
  address:shopInfo.province+shopInfo.city+shopInfo.area+shopInfo.address
 })     
},

tapMakePhoneCall:function(options){
  let shopInfo=options.currentTarget.dataset.item
  wx.makePhoneCall({  
    phoneNumber: shopInfo.link_phone,          
    success: function(res){          
       console.log(res)          
    },        
    fail: function(err){          
       console.log(err)          
    }         
  })
}, 

  gostorage(optios){
    let storage_shop_name=optios.currentTarget.dataset.item.storage_shop_name
    this.$showModal({title:"温馨提示",content: '您确认切换到【'+storage_shop_name+']】门店嘛？'}).then(res=>{
      if(res.confirm){
        getApp().storageId=optios.currentTarget.dataset.item.storageId
        wx.navigateTo({
          url: '/pagesShop/shop/index',
           success: function (e) {
            let page = getCurrentPages().pop();
            page.onShow();
          }  
        })
      }
    })
  },
  search(e){
    let search=e.detail.value
    this.setData({search})
    this.getLocation()
    
  }

  
})